<?php
class Model_Product_XML {
	protected $_XMLfile;
	protected $_tableName = 'product';
	protected $_rows = array(
		'Ц0005'=>'brand',
		'Ц0008'=>'category',
		'Ц0007'=>'class',
		'Ц0011'=>'material',
		'Ц0012'=>'gender',
		'Ц0009'=>'size',
		'Ц0006'=>'season',
		'Ц0010'=> 'delivery',
	     
	);
	public function __construct()
	{
		$this->_XMLfile  =  $_SERVER['DOCUMENT_ROOT'] . '/data/products.xml';
	}
	
	public function XmlToMysql($start, $end)
	{
		
		if($start == 0){
			$this->_truncate();
		}
		$catalog =  $this->getCatalogNode();
		$i = 0;
		$sql = "INSERT into $this->_tableName (id";
		foreach ($this->_rows as $row){
			$sql.=",$row";
		}
		$sql .= ' ,category_id';
		$sql .= ") VALUES ";
		foreach($catalog as $item){
			$products =  $catalog->item($i)->getElementsByTagName('Товар');
			$j = $start;
			
			foreach ($products as $product)
			{	
				if($i!=0){
					$sql.=',';
				}
					$n = 0;
					if(!(bool)$products->item($j)) return $i;
					$params = $products->item($j)->getElementsByTagName('ЗначениеСвойства');
					
					$id = $products->item($j)->getAttribute('ИдентификаторВКаталоге');
					$category_id = explode('ID',$products->item($j)->getAttribute('Родитель'));
					$category_id = $category_id[1];
					$attribs = array();
					foreach($params as $param){
						$attr = $params->item($n)->getAttribute('ИдентификаторСвойства');
						$name = $params->item($n)->getAttribute('Значение');
						if($attr){
							$attribs[$attr] = $name;
						}
						$n++;
					}
					ksort($attribs);
					$sql .= '(';
					$m=0;
					$sql .= "'$id'";
					foreach($this->_rows as $key=>$value){
						if(key_exists($key, $attribs)){
							$sql .= "," . Zend_Registry::get('db')->quote($attribs[$key]);
						} else {
							$sql .=",''";
						}
						$m++;
					}
					$sql.=",'$category_id'";
					$sql .=')';
				$i++;
				$j++;
				if($j>$end) break;
			}
		}
	
		$sql .=';';
		Zend_Registry::get('db')->query($sql);
		return $i;
	}
	
	protected function _truncate()
	{
		Zend_Registry::get('db')->getConnection()->exec('truncate table ' . $this->_tableName);
	}
	
	public function updatePices()
	{
	    
	    $dom = new DOMDocument();
        $dom->load($this->_XMLfile);
        $elements = $dom->getElementsByTagName('КоммерческаяИнформация');
        $catalog = $elements->item(0)->getElementsByTagName('ПакетПредложений');
	    $i=0;
	    foreach ($catalog as $item){
	       
	        $products =  $catalog->item($i)->getElementsByTagName('Предложение');
	        $j=0;
	        foreach($products as $product){
	            
	            $price = $products->item($j)->getAttribute('Цена');
	            $id = explode('ID',$products->item($j)->getAttribute('ИдентификаторТовара'));
	            $id = $id[1];
	            if($price != '' && $id != ''){
    	            $sql = "UPDATE product set price = $price where id = $id";
                    Zend_Registry::get('db')->query($sql);
	            }
	            $j++;
	        }
	        $i++;
	    }
	}
	
	public function getCatalogNode()
	{
		$dom = new DOMDocument();
		$dom->load($this->_XMLfile);
		$elements = $dom->getElementsByTagName('КоммерческаяИнформация');
		return $elements->item(0)->getElementsByTagName('Каталог');
	}
}